﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq.Expressions;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using VisualWip.Core.Utils;

namespace VisualWip.TFS2010Providers
{
    public abstract class QueryItemViewModel : INotifyPropertyChanged
    {
        private readonly QueryItemViewModel _parent;
        private bool _isExpanded;
        private bool _isSelected;

        protected QueryItemViewModel(QueryItem queryItem, QueryItemViewModel parent)
        {
            _parent = parent;

            Id = queryItem.Id;
            Name = queryItem.Name;
            Children = new ObservableCollection<QueryItemViewModel>();
        }

        public string Name { get; set; }

        public ObservableCollection<QueryItemViewModel> Children { get; private set; }

        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value != _isSelected)
                {
                    _isSelected = value;
                    ExpandParent();
                    OnPropertyChanged(this, () => IsSelected);
                }
            }
        }

        protected void ExpandParent()
        {
            if (_parent != null)
            {
                IsExpanded = true;
                _parent.ExpandParent();
            }
        }

        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                if (value != _isExpanded)
                {
                    _isExpanded = value;
                    OnPropertyChanged(this, () => IsExpanded);
                }
            }
        }

        public Guid Id { get; set; }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        protected void OnPropertyChanged<T>(object sender, Expression<Func<T>> property)
        {
            PropertyHelper.OnPropertyChanged(sender, PropertyChanged, property);
        }

    }
}